/**
* <b>Description:</b><br> 
* @author Reading.Reagan</a>
* @version 1.0
* @Note
* <b>ProjectName:</b> member
* <br><b>PackageName:</b> com.jingyanzi.client
* <br><b>ClassName:</b> package-info
* <br><b>Date:</b> 2018年9月28日 下午3:12:08
*/
package com.jingyanzi.member.client;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.member.api.DicClientAPI;
import com.jingyanzi.member.domain.CustomerApiDO;
import com.jingyanzi.member.domain.CustomerDO;
import com.jingyanzi.member.domain.LeadDO;

import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.service.IAnalysisService;
import com.jingyanzi.member.service.ICustomerService;
import com.jingyanzi.member.util.BeanUtilsExtends;
import com.jingyanzi.sys.domain.RegionDO;

@RestController
@RequestMapping("/api")
public class CustomerClientImpl {

	private static Logger logger = Logger.getLogger(CustomerClientImpl.class);

	@Autowired
	private IAnalysisService analysisService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private DicClientAPI dicClientAPI;

	
	@GetMapping("/customer/{id}")
	public CustomerDO get(@PathVariable(value = "id") Integer id) {
		ResultDO result = customerService.get(id);
		return (CustomerDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
	}

	/**
	 * 通过客户id、线索id获取客户信息及拍摄主体信息<b>Description:通过客户id、线索id获取客户信息及拍摄主体信息(订单接口)</b>
	 * <br>
	 * 
	 * @param query
	 * @return ResultDO List<CustomerDO>
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年7月13日 下午2:11:53 <br>
	 *       <b>Version:</b> 1.0
	 */
	
	@GetMapping("/customer/{id}/{leadId}")
	public CustomerApiDO getCustomerInfo(@PathVariable(value = "id") Integer id,
			@PathVariable(value = "leadId") Integer leadId) {

		CustomerApiDO customerApiDO = new CustomerApiDO();

		CustomerDO customerDO = null;
		LeadDO leadDO = null;
		if (leadId > 0) {
			// 线索
			ResultDO result = analysisService.get(leadId);
			if (result.isSuccess()) {
				leadDO = (LeadDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
				customerDO = leadDO.getCustomerDO();
			} else {
				logger.error("线索信息查询失败， id=" + leadId);
				return null;
			}
		} else if (id > 0) {
			// 客户
			ResultDO result = customerService.get(id);
			if (!result.isSuccess()) {
				logger.error("客户信息查询失败， id=" + id);
				return null;
			}
			Map<String, Object> map = (Map<String, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY);
			customerDO = (CustomerDO) map.get("customerDO");
		}

		if (customerDO == null) {
			return null;
		}
		if (!BeanUtilsExtends.copyProperties(customerApiDO, customerDO)) {
			logger.error("客户信息拷贝失败， id=" + id);
			return null;
		}
		// 线索数据
		customerApiDO.setLeadId(leadId);
		if (null != leadDO) {
			customerApiDO.setLeadNo(leadDO.getLeadNo());
			customerApiDO.setBusinessType(leadDO.getBusinessTypeName());
			customerApiDO.setBusinessFamilyID(leadDO.getBusinessFamilyID());
			customerApiDO.setBusinessFamilyName(leadDO.getBusinessFamily().getName());
			customerApiDO.setGenderName(leadDO.getBusinessFamily().getGenderName());
			customerApiDO.setBirthday(leadDO.getBusinessFamily().getBirthday());
			customerApiDO.setWeddingDate(leadDO.getWeddingdate());
			customerApiDO.setExpectDate(leadDO.getExpectdate());
		}

		// 客户数据
		customerApiDO.setMobileNumber(customerDO.getMobileFemaleNumber());
		customerApiDO.setQq(customerDO.getFirstFamilyDO().getQq());
		customerApiDO.setWechat(customerDO.getFirstFamilyDO().getWechat());
		customerApiDO.setGenderName(customerDO.getFirstFamilyDO().getGenderName());

		// 所在区域汉字
		if (StringUtils.isNotEmpty(customerDO.getProvince())) {
			RegionDO regionDO = dicClientAPI.getRegionDO(customerDO.getProvince(),
					StringUtils.isNotEmpty(customerDO.getCity()) ? customerDO.getCity() : "all",
					StringUtils.isNotEmpty(customerDO.getDistrict()) ? customerDO.getDistrict() : "all");
			customerApiDO.setProvinceStr(regionDO.getProvince());
			customerApiDO.setCityStr(regionDO.getCity());
			customerApiDO.setDistrictStr(regionDO.getDistrict());
		}

		return customerApiDO;
	}

}